import time

from usb2gpio import usb_device, usb2gpio
import ctypes

DevHandles = (ctypes.c_int * 20)()
DevHandle = 0
# Scan device
ret = usb_device.USB_ScanDevice(ctypes.byref(DevHandles))
if ret == 0:
    print("No device connected!")
    exit()
else:
    print("Have %d device connected!" % ret)
    DevHandle = DevHandles[0]  # 选择设备0
# Open device
ret = usb_device.USB_OpenDevice(DevHandle)
if bool(ret):
    print("Open device success!")
else:
    print("Open device faild!")
    exit()

print(DevHandle)

pin_btn = 0b10
pin_blue = 0
pin_value = ctypes.c_uint(0)
# usb_device.USB_CloseDevice(DevHandle)
usb2gpio.GPIO_SetInput(DevHandle, pin_btn, 0)
usb2gpio.GPIO_SetOutput(DevHandle, pin_blue, 0)
count = 0
while True:
    usb2gpio.GPIO_Read(DevHandle, pin_btn, ctypes.byref(pin_value))
    time.sleep(0.5)
    if pin_value:
        print(f'yes')
        usb2gpio.GPIO_Write(DevHandle, pin_blue, 0b01)
    else:
        print(f'no')
        usb2gpio.GPIO_Write(DevHandle, pin_blue, 0)
    # ret= usb2gpio.GPIO_Write(DevHandle,pin_blue,0b01)
    # print(ret)
